package es.aqua.aquac.bl.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import es.aqua.aquac.bl.interfaces.GeneralBS;
import es.aqua.aquac.exception.GeneralBusinessException;
import es.aqua.aquac.il.dal.interfaces.GeneralDAO;
import es.aqua.aquac.util.Utiles;

public class GeneralBSImpl<E> implements GeneralBS<E> {

	private GeneralDAO<E> generalDAO;

	public GeneralDAO<E> getGeneralDAO() {
		return generalDAO;
	}

	public void setGeneralDAO(GeneralDAO<E> generalDAO) {
		this.generalDAO = generalDAO;
	}

	public E insert(E entity) throws GeneralBusinessException {

		try {
			Utiles.setearAuditoriaAlta(entity);
			generalDAO.insert(entity);

			return entity;
		} catch (Exception e) {
			throw new GeneralBusinessException("BL-INSERT", "Se ha producido un error al insertar el Registro de tipo '"
					+ entity.getClass().getCanonicalName() + "'");
		}

	}

	public void update(E entity) throws GeneralBusinessException {
		try {
			Utiles.setearAuditoriaModificacion(entity, false);
			generalDAO.update(entity);
		} catch (Exception e) {
			throw new GeneralBusinessException("BL-UPDATE", "Se ha producido un error al insertar el Registro de tipo '"
					+ entity.getClass().getCanonicalName() + "'");
		}

	}

	public void remove(E entity) throws GeneralBusinessException {
		try {
			Utiles.setearAuditoriaModificacion(entity, true);
			generalDAO.remove(entity);
		} catch (Exception e) {
			throw new GeneralBusinessException("BL-REMOVE", "Se ha producido un error al borrar el Registro de tipo '"
					+ entity.getClass().getCanonicalName() + "'");
		}

	}

	public E findById(Serializable id) throws GeneralBusinessException {
		try {
			return (E) generalDAO.findById(id);
		} catch (Exception e) {
			throw new GeneralBusinessException("BL-FINDBYID", "Se ha producido un error al buscar por ID.");
		}
	}

	public List<E> findByCriteria(E entity, String prop, Boolean asc) throws GeneralBusinessException {
		try {
			return generalDAO.findByCriteria(entity, prop, asc);
		} catch (Exception e) {
			throw new GeneralBusinessException("BL-FINDBYCRITERIA", "Se ha producido un error al buscar por Criterios. Entidad '"
					+ entity.getClass().getCanonicalName() + "'");
		}
	}

	public List<E> findAll() throws GeneralBusinessException {
		List<E> entities = new ArrayList<E>();
		try {
			entities = generalDAO.findAll();
		} catch (Exception e) {
			throw new GeneralBusinessException("BL-FINDALL", e.getMessage());
		}
		return entities;
	}

	public List<E> findAllOrderBy(String prop, Boolean asc) throws GeneralBusinessException {
		List<E> entities = new ArrayList<E>();
		try {
			entities = generalDAO.findAllOrderBy(prop, asc);
		} catch (Exception e) {
			throw new GeneralBusinessException("BL-FINDALLORDERBY", e.getMessage());
		}
		return entities;
	}

	public List<E> findByProperty(String prop, String value) throws GeneralBusinessException {
		List<E> entities = new ArrayList<E>();
		try {
			entities = generalDAO.findByProperty(prop, value);
		} catch (Exception e) {
			throw new GeneralBusinessException("BL-DAL-FINDBYPROPERTY", e.getMessage());
		}
		return entities;

	}

}
